package com.mh.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mh.domain.AppUser;
import com.mh.web.utils.Utils;

/**
 * DAO to manage AppUser entities.
 * 
 */
@Repository("AppUserDAO")
@Transactional
public class AppUserDAOImpl extends AbstractJpaDao<AppUser> implements
		AppUserDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 * 
	 * @generated
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { AppUser.class }));
	/**
	 * EntityManager injected by Spring for persistence unit mhtest
	 *
	 * 
	 * @generated
	 */
	@PersistenceContext(unitName = "mhtest")
	private EntityManager entityManager;

	/**
	 * Instantiates a new AppUserDAOImpl
	 *
	 * 
	 * @generated
	 */
	public AppUserDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 * 
	 * @generated
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 * 
	 * @generated
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAppUserByUserNameContaining
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByUserNameContaining(String userName) throws DataAccessException {

		return findAppUserByUserNameContaining(userName, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByUserNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByUserNameContaining(String userName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByUserNameContaining", startResult, maxRows, userName);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserBySecretPasswordContaining
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserBySecretPasswordContaining(String secretPassword) throws DataAccessException {

		return findAppUserBySecretPasswordContaining(secretPassword, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserBySecretPasswordContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserBySecretPasswordContaining(String secretPassword, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserBySecretPasswordContaining", startResult, maxRows, secretPassword);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByFullName
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByFullName(String fullName) throws DataAccessException {

		return findAppUserByFullName(fullName, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByFullName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByFullName(String fullName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByFullName", startResult, maxRows, fullName);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByFullNameContaining
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByFullNameContaining(String fullName) throws DataAccessException {

		return findAppUserByFullNameContaining(fullName, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByFullNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByFullNameContaining(String fullName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByFullNameContaining", startResult, maxRows, fullName);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllAppUsers
	 *
	 */
	@Transactional
	public Set<AppUser> findAllAppUsers() throws DataAccessException {

		return findAllAppUsers(-1, -1);
	}

	/**
	 * JPQL Query - findAllAppUsers
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAllAppUsers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAppUsers", startResult, maxRows);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}
	
	
	
	public List<AppUser> findAllAppUsers(AppUser appuser, int startResult, int maxRows) throws DataAccessException{		
		List lst = entityManager.createQuery("select myAppUser from AppUser myAppUser").getResultList();		
		return (List<AppUser>)lst;
	}
	

	/**
	 * JPQL Query - findAppUserByEmail
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByEmail(String email) throws DataAccessException {

		return findAppUserByEmail(email, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByEmail
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByEmail(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByEmail", startResult, maxRows, email);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByState
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByState(Boolean state) throws DataAccessException {

		return findAppUserByState(state, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByState
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByState(Boolean state, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByState", startResult, maxRows, state);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByEmailContaining
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByEmailContaining(String email) throws DataAccessException {

		return findAppUserByEmailContaining(email, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByEmailContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByEmailContaining(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByEmailContaining", startResult, maxRows, email);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByBirthday
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByBirthday(java.util.Date birthday) throws DataAccessException {

		return findAppUserByBirthday(birthday, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByBirthday
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByBirthday(java.util.Date birthday, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByBirthday", startResult, maxRows, birthday);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByUserName
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserByUserName(String userName) throws DataAccessException {

		return findAppUserByUserName(userName, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByUserName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserByUserName(String userName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserByUserName", startResult, maxRows, userName);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserById
	 *
	 */
	@Transactional
	public AppUser findAppUserById(Integer id) throws DataAccessException {

		return findAppUserById(id, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserById
	 *
	 */

	@Transactional
	public AppUser findAppUserById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAppUserById", startResult, maxRows, id);
			return (com.mh.domain.AppUser) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAppUserBySecretPassword
	 *
	 */
	@Transactional
	public Set<AppUser> findAppUserBySecretPassword(String secretPassword) throws DataAccessException {

		return findAppUserBySecretPassword(secretPassword, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserBySecretPassword
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<AppUser> findAppUserBySecretPassword(String secretPassword, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAppUserBySecretPassword", startResult, maxRows, secretPassword);
		return new LinkedHashSet<AppUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findAppUserByPrimaryKey
	 *
	 */
	@Transactional
	public AppUser findAppUserByPrimaryKey(Integer id) throws DataAccessException {

		return findAppUserByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findAppUserByPrimaryKey
	 *
	 */

	@Transactional
	public AppUser findAppUserByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAppUserByPrimaryKey", startResult, maxRows, id);
			return (com.mh.domain.AppUser) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(AppUser entity) {
		return true;
	}
	
	
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<AppUser> findAppUser(AppUser appuser) throws DataAccessException {
		String queryFilter = "select myAppUser from AppUser myAppUser where 1=1 ";

		if(Utils.isNullOrEmpty(appuser.getId()))
			queryFilter += "and myAppUser.id= " + appuser.getId();
		
		if(Utils.isNullOrEmpty(appuser.getUserName()))
			queryFilter +="and upper(myAppuser.userName) like '%"+appuser.getUserName().toUpperCase()+"%'";
		
		if(Utils.isNullOrEmpty(appuser.getFullName()))
			queryFilter +="and upper(myAppuser.fullName) like '%"+appuser.getFullName().toUpperCase()+"%'";
		
		if(Utils.isNullOrEmpty(appuser.getEmail()))
			queryFilter +="and upper(myAppuser.email) like '%"+appuser.getEmail().toUpperCase()+"%'";
		
		if(Utils.isNullOrEmpty(appuser.getId()))
			queryFilter += "and myAppUser.state= " + appuser.getId();
				
		if (appuser.getBirthday() != null ){
			queryFilter += " and to_char(myAppUser.birthday,'dd-mm-yyyy') = '" +  appuser.getBirthdayString() + "'"; 
		}
				
		return entityManager.createQuery(queryFilter).getResultList();		
	}	
	
	
}//class
